<?php

class Coevent extends AppModel {

    var $name = 'Coevent';
    var $primaryKey = 'EventID';
    var $displayField = 'Event';
    var $order = "Coevent.EventDate DESC";
    var $validate = array(
        'EventID' => array(
            'notempty' => array(
                'rule' => array('notempty'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
            'numeric' => array(
                'rule' => array('numeric'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'ComID' => array(
            'numeric' => array(
                'rule' => array('numeric'),
                'message' => 'Company cannot be left empty',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'Event' => array(
            'notempty' => array(
                'rule' => array('notempty'),
                'message' => 'Event cannot be left empty',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
    );
    //The Associations below have been created with all possible keys, those that are not needed can be removed

    var $belongsTo = array(
        'Codetail' => array(
            'className' => 'Codetail',
            'foreignKey' => 'ComID',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );

    function import($filename) {
        $error = false;

        // open the file
        $handle = fopen($filename, "r");

        // read the 1st row as headings
        $header = fgetcsv($handle);

        // create a message container
        $return = array(
            'messages' => array(),
            'errors' => array(),
        );
        $i = 0;
        // read each data row in the file
        while (($row = fgetcsv($handle)) !== FALSE) {
            $i++;
            $data = array();

            // for each header field 
            foreach ($header as $k => $head) {
                // get the data field from Model.field
                //echo $head;exit;
                if (strpos($head, '.') !== false) {
                    $h = explode('.', $head);
                    $data[$h[0]][$h[1]] = (isset($row[$k])) ? $row[$k] : '';
                }
                // get the data field from field
                else {
                    $data['Coevent'][$head] = (isset($row[$k])) ? $row[$k] : '';
                }
            }

            // see if we have an id 			
            $id = isset($data['Coevent']['EventID']) ? $data['Coevent']['EventID'] : 0;

            // we have an id, so we update
            if ($id) {
                // there is 2 options here, 
                // option 1:
                // load the current row, and merge it with the new data
                //$this->recursive = -1;
                //$post = $this->read(null,$id);
                //$data['Post'] = array_merge($post['Post'],$data['Post']);
                // option 2:
                // set the model id
                $this->id = $id;
            }

            // or create a new record
            else {
                $this->create();
            }

            // see what we have
            //debug($data);exit;
            //pr($data);
            // validate the row
            $this->set($data);
            if (!$this->validates()) {
                //$this->_flash('warning');
                $return['errors'][] = __(sprintf('Coevent for Row %d failed to validate.', $i), true);
                $error = true;
            }
            //debug($this->data);exit;
            // save the row
            if (!$error && !$this->save($data)) {
                $return['errors'][] = __(sprintf('Coevent for Row %d failed to save.', $i), true);
                $error = true;
            }

            // success message!
            if (!$error) {
                $return['messages'][] = __(sprintf('Coevent for Row %d was saved.', $i), true);
            }
            //if($i==10){echo"sahabj"; break;}
        }

        // close the file
        fclose($handle);

        // return the messages
        return $return;
    }

    function nameChanged($data, $comid) {
        $e=0;
        foreach($data as $coevent){
            $coevents['Coevent'][$e]['ComID']=$comid;
            $coevents['Coevent'][$e]['Event']=$coevent['Event'];
            $coevents['Coevent'][$e]['EventDate']=$coevent['EventDate'];
            $coevents['Coevent'][$e]['Position']=$coevent['Position'];
            $coevents['Coevent'][$e]['delistDate']=$coevent['delistDate'];
            $coevents['Coevent'][$e]['FileName']=$coevent['FileName'];
            $coevents['Coevent'][$e]['TimeStamp']=$coevent['TimeStamp'];
            $coevents['Coevent'][$e]['_declaration']=$coevent['_declaration'];
            $coevents['Coevent'][$e]['_flagA']=$coevent['_flagA'];
            $coevents['Coevent'][$e]['Note']=$coevent['Note'];
            $coevents['Coevent'][$e]['EventDateFormat']=$coevent['EventDateFormat'];
            $e++;
        }
        $this->create();
        $this->saveAll($coevents['Coevent']);
    }

}
